package com.corecode.common.ldap.ldap;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import com.corecode.common.ldap.AbstractLdap;
import com.corecode.common.ldap.field.DummyGroupLdapInfo;
import com.corecode.common.ldap.field.UserGroupLdapInfo;
import com.corecode.common.ldap.field.UserLdapInfo;
import com.corecode.common.ldap.util.LdapUtil;

/**
 * IBM TDS 
 * Copyright by (2010)
 * author luowei
 * Created on Nov 27, 2010 10:30:16 AM
 */
public class IBMTDSFieldLdap extends AbstractLdap {
	
	public IBMTDSFieldLdap() {}

	public IBMTDSFieldLdap(String dn, String pwd, String host) {
		this.dn = dn;
		this.pwd = pwd;
		this.host = host;
	}
	
	private String searchRelatingGroup(String dn) throws NamingException {
/*		String name = "cn=groups";
		String matchingAttributes = "uniqueMember=" + dn;
		String[] returnAttrs = { "ibm-entryuuid", "*" }; 
		Vector<SearchResult> vector = this.searchAttr(name,matchingAttributes,returnAttrs);*/
		return "";
	}

	@SuppressWarnings({ "unused", "unchecked" })
	private Vector<SearchResult> searchAttr(String name,String matchingAttributes,String[] returnAttrs)
			throws NamingException {
		Vector<SearchResult> vector = new Vector<SearchResult>();
		SearchControls searchControls = new SearchControls();
		searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		searchControls.setReturningAttributes(returnAttrs);
		searchControls.setCountLimit(LdapUtil.getLdapLimitAmount());
		NamingEnumeration enumResults = dctx.search(name,matchingAttributes, searchControls);
		while (enumResults.hasMoreElements()) {
			SearchResult sr = (SearchResult) enumResults.next();
			vector.add(sr);
		}
		return vector;
	}

	@Override
	public Vector<DummyGroupLdapInfo> getAllDummyGroupInfo()
			throws NamingException {
		return v_DummyGroupInfo;
	}

	@Override
	public Vector<UserGroupLdapInfo> getAllUserGroupInfo()
			throws NamingException {
		UserGroupLdapInfo adInfo = null;
		String matchingAttributes = "objectClass=null";
		String[] returnAttrs = { "ibm-entryuuid", "*" };
		Vector<SearchResult> vector = searchAttr(matchingAttributes,returnAttrs);
		if(vector != null) {
			for (SearchResult sr : vector) {
				adInfo = new UserGroupLdapInfo();
				adInfo.setDn( sr.getNameInNamespace());
				Attributes attrs = sr.getAttributes();
				if (attrs != null) {
					if(attrs.get("cn") != null) {
						adInfo.setUserGroupName(attrs.get("cn").get().toString().trim().toLowerCase());
					}
					adInfo.setObjectGUID(attrs.get("ibm-entryuuid").get().toString().trim().toLowerCase());
					adInfo.setHost(host);
				}
				v_UserGroupInfo.add(adInfo);
			}
		} 
		
		return v_UserGroupInfo;
	}

	@Override
	public Vector<UserLdapInfo> getAllUserInfo() throws NamingException {
		UserLdapInfo adInfo = null;
		String matchingAttributes = "uid=*";
		String[] returnAttrs = { "ibm-entryuuid", "*" };
		Vector<SearchResult> vector = searchAttr(matchingAttributes,returnAttrs);
		if(vector != null) {
			for (SearchResult sr : vector) {
				adInfo = new UserLdapInfo();
				adInfo.setDn(sr.getNameInNamespace());
				Attributes attrs = sr.getAttributes();
				if (attrs != null) {
					adInfo.setUserID(attrs.get("uid").get().toString().trim().toLowerCase());
					String userName = attrs.get("givenname") != null?
								attrs.get("givenname").get().toString() : attrs.get("uid").get().toString();
					adInfo.setUserName(userName);
					adInfo.setTitle(attrs.get("title") != null?attrs.get("title").get().toString():"");
					adInfo.setObjectGUID(attrs.get("ibm-entryuuid").get().toString().trim().toLowerCase());
					List<String> relaList = new ArrayList<String>();
					relaList.add(0,searchRelatingGroup(adInfo.getDn()));
					adInfo.setRelatingUserGroup(relaList);
					adInfo.setHost(host);
				}
				v_UserInfo.add(adInfo);
			}
		}
		
		return v_UserInfo;
	}
}
